public Class AccountWebAddressTriggerHandler{

static Set<Id> accWebAddressupdateid=new Set<Id>();
static Set<Id> accWebAddressinsertid=new Set<Id>();

public void AccountWebAddressInsert(List<Web_Address__c> accWebAddresslist){
     Set<Id> WebAddressid=new Set<Id>();
     Set<Id> accid=new Set<Id>();
    List<Web_Address__c> oldaccountWebAddresslist=new List<Web_Address__c>();
    List<Web_Address__c> oldaccountWebAddressupdatelist=new List<Web_Address__c>();
     Web_Address__c WebAddress=new Web_Address__c();
    
    if(accWebAddresslist!=null&&accWebAddresslist.size()>0){   
     WebAddress= accWebAddresslist[0];
        for(Web_Address__c accWebAddress: accWebAddresslist){
            WebAddressid.add(accWebAddress.Id);
            accid.add(accWebAddress.Affiliate_Name__c);
        }       
        oldaccountWebAddresslist=[select Affiliate_Name__c,Preferred__c from Web_Address__c where Preferred__c=true AND Affiliate_Name__c IN:accid AND Id NOT IN:WebAddressid];
        
    for(Web_Address__c accWebAddress: accWebAddresslist){
                      
            system.debug('&&oldaccountWebAddresslist'+ oldaccountWebAddresslist);
            system.debug('&&accWebAddresslist'+ accWebAddresslist);
            if(oldaccountWebAddresslist!=null && oldaccountWebAddresslist.size()>0){                 
                      
                   if(accWebAddress.Preferred__c==true) {      
                    for(Web_Address__c oldWebAddress : oldaccountWebAddresslist){
                             oldWebAddress.Preferred__c=false;
                             oldaccountWebAddressupdatelist.add(oldWebAddress);
                             accWebAddressinsertid.add(oldWebAddress.Id);       
                    }
                  }
                    
            }else{
                WebAddress.Preferred__c=true;
            }                             
        }
        
        system.debug('&&oldaccountWebAddressupdatelist'+ oldaccountWebAddressupdatelist);
      if(!oldaccountWebAddressupdatelist.IsEmpty()){
          update oldaccountWebAddressupdatelist;
      }
    
    }
    
 }
public void AccountWebAddressUpdate(List<Web_Address__c> newaccWebAddresslist,List<Web_Address__c> oldaccWebAddresslist){
     Set<Id> WebAddressid=new Set<Id>();
     Set<Id> accid=new Set<Id>();
    List<Web_Address__c> oldaccountWebAddresslist=new List<Web_Address__c>();
    List<Web_Address__c> oldaccountWebAddressupdatelist=new List<Web_Address__c>();
    
    if(newaccWebAddresslist!=null&&newaccWebAddresslist.size()>0){    
        for(Web_Address__c accWebAddress: newaccWebAddresslist){
            WebAddressid.add(accWebAddress.Id);
            accid.add(accWebAddress.Affiliate_Name__c);
        }       
        oldaccountWebAddresslist=[select Affiliate_Name__c,Preferred__c from Web_Address__c where Preferred__c=true AND Affiliate_Name__c IN:accid AND Id NOT IN:WebAddressid];
        
        
            for(Web_Address__c newaccWebAddress: newaccWebAddresslist){
                for(Web_Address__c oldaccWebAddress: oldaccWebAddresslist){
                    if((newaccWebAddress.Preferred__c==true && oldaccWebAddress.Preferred__c==false && newaccWebAddress.Affiliate_Name__c==oldaccWebAddress.Affiliate_Name__c)){ 
                        for(Web_Address__c accvalue: oldaccountWebAddresslist){
                              accvalue.Preferred__c=false;        
                              oldaccountWebAddressupdatelist.add(accvalue);
                              accWebAddressupdateid.add(accvalue.Id);                            
                          } 
                    } else{
                    for(Web_Address__c accval: newaccWebAddresslist){
                          
                            if(accWebAddressupdateid.contains(accval.id) ){
                            }else{
                            if(accWebAddressinsertid.IsEmpty()&&(newaccWebAddress.Preferred__c==false&& oldaccWebAddress.Preferred__c==true))
                             trigger.new[0].addError('cannot update');  
                            }
                        }
                    
                    }
                
                }
            }
           
        }
        
        if(!oldaccountWebAddressupdatelist.isEmpty()){
                try{
                    
                    update oldaccountWebAddressupdatelist;
                }catch(Exception e){
                    system.debug('# Exception while updating : '+e);
                }
            }                                  
    }
    public void ContactWebAddressDelete(List<Web_Address__c> accountWebAddresslist){
        if(accountWebAddresslist!=null&&accountWebAddresslist.size()>0){
            system.debug('@@accountWebAddresslist '+accountWebAddresslist);
            for(Web_Address__c accWebAddress: accountWebAddresslist){
            
            if(accWebAddress.Preferred__c==true){
                    trigger.old[0].addError('Cannot delete prefered record');  
                }
            }
        
        }

    }
    
/************************************************************************
        Purpose     : Test Class                  
        Parameters  : None
        Returns     : None
***********************************************************************/
    @isTest(SeeAllData=false)
     static public void testAccountWebAddressTriggerHandler() {
         
         PIMS_Reserved_Id__c pimsid=new PIMS_Reserved_Id__c();
         pimsid.PIMS_Id__c='0000-0000';
         pimsid.Used__c=false;
         insert pimsid;
         
         Account acc=new Account();
         acc.Name='test';
         acc.Type='SSO';
         insert acc;
         
         Web_Address__c accWebAddress=new Web_Address__c();
         accWebAddress.Affiliate_Name__c=acc.id;
         accWebAddress.Type__c='WebAddress at Home';
         accWebAddress.Web_Address__c='test@a.com';
         accWebAddress.Preferred__c=true;
         insert accWebAddress;
         update accWebAddress;
         
         Web_Address__c accWebAddress0=new Web_Address__c();
         accWebAddress0.Affiliate_Name__c=acc.id;
         accWebAddress0.Type__c='WebAddress at Home';
         accWebAddress0.Web_Address__c='test@a.com';
         accWebAddress0.Preferred__c=true;
         insert accWebAddress0;
         
         Web_Address__c accWebAddress1=new Web_Address__c();
         accWebAddress1.Affiliate_Name__c=acc.id;
         accWebAddress1.Type__c='WebAddress at Home';
         accWebAddress1.Web_Address__c='test@a.com';
         accWebAddress1.Preferred__c=false;
         insert accWebAddress1;
         accWebAddress1.Preferred__c=true;
         update accWebAddress1;
         
         Web_Address__c accWebAddress2=new Web_Address__c();
         accWebAddress2.Affiliate_Name__c=acc.id;
         accWebAddress2.Type__c='WebAddress at Home';
         accWebAddress2.Web_Address__c='test@a.com';
         accWebAddress2.Preferred__c=false;
         insert accWebAddress2;
         delete accWebAddress2;
     }   
}